<template>
	<el-main class="bg edit_wrap comtable_e">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce">
							<el-col v-if="$check_field('get','merchant_users') || $check_field('add','merchant_users') || $check_field('set','merchant_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家用户" prop="merchant_users">
																					<div v-if="user_group !== '管理员'">
							{{ get_user_session_merchant_users(form['merchant_users']) }}
							<el-select v-if="(form['homestay_information_id'] && $check_field('set','merchant_users')) || (!form['homestay_information_id'] && $check_field('add','merchant_users'))" id="merchant_users" v-model="form['merchant_users']" :disabled="disabledObj['merchant_users_isDisabled']">
								<el-option v-for="o in list_user_merchant_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','merchant_users')" id="merchant_users" v-model="form['merchant_users']" :disabled="true">
								<el-option v-for="o in list_user_merchant_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="merchant_users" v-model="form['merchant_users']" :disabled="disabledObj['merchant_users_isDisabled']">
							<el-option v-for="o in list_user_merchant_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','name_of_homestay') || $check_field('add','name_of_homestay') || $check_field('set','name_of_homestay')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="民宿名称" prop="name_of_homestay">
															<el-input id="name_of_homestay" v-model="form['name_of_homestay']" placeholder="请输入民宿名称"
							  v-if="(form['homestay_information_id'] && $check_field('set','name_of_homestay')) || (!form['homestay_information_id'] && $check_field('add','name_of_homestay'))" :disabled="disabledObj['name_of_homestay_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','name_of_homestay')">{{form['name_of_homestay']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','types_of_homestays') || $check_field('add','types_of_homestays') || $check_field('set','types_of_homestays')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="民宿类型" prop="types_of_homestays">
											<el-select id="types_of_homestays" v-model="form['types_of_homestays']"
						v-if="(form['homestay_information_id'] && $check_field('set','types_of_homestays')) || (!form['homestay_information_id'] && $check_field('add','types_of_homestays'))">
						<el-option v-for="o in list_types_of_homestays" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','types_of_homestays')">{{form['types_of_homestays']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','room_area') || $check_field('add','room_area') || $check_field('set','room_area')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="房间面积" prop="room_area">
															<el-input id="room_area" v-model="form['room_area']" placeholder="请输入房间面积"
							  v-if="(form['homestay_information_id'] && $check_field('set','room_area')) || (!form['homestay_information_id'] && $check_field('add','room_area'))" :disabled="disabledObj['room_area_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','room_area')">{{form['room_area']}}</div>
											</el-form-item>
			</el-col>
      <el-col v-if="$check_field('get','parking') || $check_field('add','parking') || $check_field('set','parking')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="停车场" prop="parking">
          <el-select id="parking" v-model="form['parking']"
                     v-if="(form['homestay_information_id'] && $check_field('set','parking')) || (!form['homestay_information_id'] && $check_field('add','parking'))">
            <el-option v-for="o in list_parking" :key="o" :label="o" :value="o">
            </el-option>
          </el-select>
          <div v-else-if="$check_field('get','parking')">{{form['parking']}}</div>
        </el-form-item>
      </el-col>

      <el-col v-if="$check_field('get','is_cook') || $check_field('add','is_cook') || $check_field('set','is_cook')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="能否做饭" prop="is_cook">
          <el-select id="is_cook" v-model="form['is_cook']"
                     v-if="(form['homestay_information_id'] && $check_field('set','is_cook')) || (!form['homestay_information_id'] && $check_field('add','is_cook'))">
            <el-option v-for="o in cook" :key="o" :label="o" :value="o">
            </el-option>
          </el-select>
          <div v-else-if="$check_field('get','is_cook')">{{form['is_cook']}}</div>
        </el-form-item>
      </el-col>

      <el-col v-if="$check_field('get','pet') || $check_field('add','pet') || $check_field('set','pet')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="能否待宠物" prop="pet">
          <el-select id="pet" v-model="form['pet']"
                     v-if="(form['homestay_information_id'] && $check_field('set','pet')) || (!form['homestay_information_id'] && $check_field('add','pet'))">
            <el-option v-for="o in pet" :key="o" :label="o" :value="o">
            </el-option>
          </el-select>
          <div v-else-if="$check_field('get','pet')">{{form['pet']}}</div>
        </el-form-item>
      </el-col>

      <el-col v-if="$check_field('get','homestay_deposit') || $check_field('add','homestay_deposit') || $check_field('set','homestay_deposit')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="民宿押金" prop="homestay_deposit">
          <el-input-number id="homestay_deposit" v-model.number="form['homestay_deposit']"
                           v-if="(form['homestay_information_id'] && $check_field('set','homestay_deposit')) || (!form['homestay_information_id'] && $check_field('add','homestay_deposit'))" :disabled="disabledObj['homestay_deposit_isDisabled']"></el-input-number>
          <div v-else-if="$check_field('get','homestay_deposit')">{{form['homestay_deposit']}}</div>
        </el-form-item>
      </el-col>
      <el-col v-if="$check_field('get','homestay_praise') || $check_field('add','homestay_praise') || $check_field('set','homestay_praise')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="民宿好评" prop="homestay_praise">
          <el-input-number id="homestay_praise" v-model.number="form['homestay_praise']"
                           v-if="(form['homestay_information_id'] && $check_field('set','homestay_praise')) || (!form['homestay_information_id'] && $check_field('add','homestay_praise'))" :disabled="disabledObj['homestay_praise_isDisabled']"></el-input-number>
          <div v-else-if="$check_field('get','homestay_praise')">{{form['homestay_praise']}}</div>
        </el-form-item>
      </el-col>
      <el-col v-if="$check_field('get','homestay_phone_number') || $check_field('add','homestay_phone_number') || $check_field('set','homestay_phone_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
        <el-form-item label="民宿电话" prop="homestay_phone_number">
          <el-input id="homestay_phone_number" v-model="form['homestay_phone_number']" placeholder="请输入民宿电话"
                    v-if="(form['homestay_information_id'] && $check_field('set','homestay_phone_number')) || (!form['homestay_information_id'] && $check_field('add','homestay_phone_number'))" :disabled="disabledObj['homestay_phone_number_isDisabled']"></el-input>
          <div v-else-if="$check_field('get','homestay_phone_number')">{{form['homestay_phone_number']}}</div>
        </el-form-item>
      </el-col>
      <el-col>
        <el-form-item label="民宿位置" prop="location_of_homestay">
          <el-select v-model="form['location_of_homestay']" filterable placeholder="请选择">
            <el-option
                v-for="item in rgionOption"
                :key="item.region_name"
                :label="item.region_name"
                :value="item.region_name">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
								<el-col v-if="$check_field('get','daily_price_of_homestay') || $check_field('add','daily_price_of_homestay') || $check_field('set','daily_price_of_homestay')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="民宿日价" prop="daily_price_of_homestay">
											<el-input-number id="daily_price_of_homestay" v-model.number="form['daily_price_of_homestay']"
						v-if="(form['homestay_information_id'] && $check_field('set','daily_price_of_homestay')) || (!form['homestay_information_id'] && $check_field('add','daily_price_of_homestay'))" :disabled="disabledObj['daily_price_of_homestay_isDisabled']"></el-input-number>
					<div v-else-if="$check_field('get','daily_price_of_homestay')">{{form['daily_price_of_homestay']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','homestay_photos') || $check_field('add','homestay_photos') || $check_field('set','homestay_photos')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="民宿照片" prop="homestay_photos">
											<el-upload :disabled="disabledObj['homestay_photos_isDisabled']" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_homestay_photos"
						:show-file-list="false" v-if="(form['homestay_information_id'] && $check_field('set','homestay_photos')) || (!form['homestay_information_id'] && $check_field('add','homestay_photos'))">
						<img id="homestay_photos" v-if="form['homestay_photos']" :src="$fullUrl(form['homestay_photos'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','homestay_photos')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['homestay_photos'])" :preview-src-list="[$fullUrl(form['homestay_photos'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','introduction_to_homestay') || $check_field('add','introduction_to_homestay') || $check_field('set','introduction_to_homestay')" :xs="24" :sm="24" :lg="24" class="el_form_editor_warp">
							<el-form-item label="民宿简介" prop="introduction_to_homestay">
								<quill-editor v-model.number="form['introduction_to_homestay']"
						v-if="(form['homestay_information_id'] && $check_field('set','introduction_to_homestay')) || (!form['homestay_information_id'] && $check_field('add','introduction_to_homestay')) ">
					</quill-editor>
					<div v-else-if="$check_field('get','introduction_to_homestay')" v-html="form['introduction_to_homestay']"></div>
				</el-form-item>
			</el-col>
								<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="审核状态" prop="examine_state">
					<el-select id="examine_state" v-model="form['examine_state']"
						v-if="(form['examine_state'] && $check_examine()) || (!form['examine_state'] && $check_examine())" :disabled="true">
						<el-option key="未审核" label="未审核" value="未审核"></el-option>
						<el-option key="已通过" label="已通过" value="已通过"></el-option>
						<el-option key="未通过" label="未通过" value="未通过"></el-option>
					</el-select>
					<div v-else>{{form["examine_state"]}}</div>
				</el-form-item>
			</el-col>





						<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp" v-if="!form['source_user_id'] && 0 > 0">
			  <el-form-item label="预定限次">
				<el-input id="limit_times" v-model="form['homestay_orders_limit_times']" placeholder="预定限制次数，0为不限"
						  v-if="$check_option('/homestay_information/view','can_limits')"
				></el-input>
				<div v-else-if="$check_action('/homestay_information/view','get')" v-html="form['homestay_orders_limit_times']"></div>
			  </el-form-item>
			</el-col>






		</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/homestay_information/view','set') || $check_action('/homestay_information/view','add') || $check_option('/homestay_information/table','examine')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		data() {
			return {
				field: "homestay_information_id",
				url_add: "~/api/homestay_information/add?",
				url_set: "~/api/homestay_information/set?",
				url_get_obj: "~/api/homestay_information/get_obj?",
				url_upload: "~/api/homestay_information/upload?",

				query: {
					"homestay_information_id": 0,
				},

				form: {
          "merchant_users": 0, // 商家用户
          "name_of_homestay":  '', // 民宿名称
          "types_of_homestays":  '', // 民宿类型
          "room_area":  '', // 房间面积
          "parking":  '', // 停车场
          "homestay_deposit":  0, // 民宿押金
          "homestay_praise":  0, // 民宿好评
          "homestay_phone_number":  '', // 民宿电话
          "location_of_homestay":  '', // 民宿位置
          "daily_price_of_homestay":  0, // 民宿日价
          "homestay_photos":  '', // 民宿照片
          "introduction_to_homestay":  '', // 民宿简介
          "examine_state": "未审核",
          "homestay_information_id": 0, // ID
          "homestay_orders_limit_times": 0, // 预定限制次数
          "is_cook":"",
          "pet":"",
        },
				disabledObj:{
          "merchant_users_isDisabled": false,
          "name_of_homestay_isDisabled": false,
          "types_of_homestays_isDisabled": false,
          "room_area_isDisabled": false,
          "parking_isDisabled": false,
          "homestay_deposit_isDisabled": false,
          "homestay_praise_isDisabled": false,
          "homestay_phone_number_isDisabled": false,
          "location_of_homestay_isDisabled": false,
          "daily_price_of_homestay_isDisabled": false,
          "homestay_photos_isDisabled": false,
          "introduction_to_homestay_isDisabled": false,
        },
        // 用户列表
				list_user_merchant_users: [],
        // 用户组
				group_user_merchant_users: "",
        rgionOption:[],

        // 民宿类型选项列表
				list_types_of_homestays: ['单人','双人','其他'],
        // 停车场选项列表
				list_parking: ['有','无'],
        cook: ['可以','不可以'],
        pet: ['可以','不可以'],
			}
		},
		methods: {
      async getRegionList(){
        var json = await this.$get("~/api/region_name/get_list");
        if(json.result && json.result.list){
          this.rgionOption = json.result.list;
        }
        else if(json.error){
          console.error(json.error);
        }

      },


				/**
			 * 获取商家用户用户列表
			 */
			async get_list_user_merchant_users() {
          var json = await this.$get("~/api/user/get_list?user_group=商家用户");
          if(json.result && json.result.list){
              this.list_user_merchant_users = json.result.list;
          }
          else if(json.error){
              console.error(json.error);
          }
			},
					/**
			 * 获取商家用户用户组
			 */
			async get_group_user_merchant_users() {
							this.form["merchant_users"] = this.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=商家用户");
				if(json.result && json.result.obj){
					this.group_user_merchant_users = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_merchant_users(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_merchant_users.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["merchant_users"] = id
									_this.disabledObj['merchant_users' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "merchant_users") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_merchant_users(id){
				var obj = this.list_user_merchant_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},


















						/**
			 * 上传民宿照片
			 * @param {Object} param 图片参数
			 */
			upload_homestay_photos(param){
									this.uploadFile(param.file, "homestay_photos");
								},





			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";

				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
																												$.db.del("form");

				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){


			},

																																																																																																async submit(param, func){
				if (!param) {
					param = this.form;
				}

				var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																																												ret = this.events("submit_main", pm, func);
				}
				return ret;
			},

			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {

																																																																																																																																										return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/homestay_information/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/homestay_information/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/homestay_information/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/homestay_information/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/homestay_information/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
      this.get_list_user_merchant_users();
      this.get_group_user_merchant_users();
      this.getRegionList();
      },
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}

	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}





</style>
